package cn.biplam.back.operator.mdManage;

import java.util.ArrayList;

import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.opcode.mdManage.TypeCode.DatabaseTypeCode;
import cn.biplam.common.struc.MdStruc;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 * 数据库类型表数据操作类
 * @author XiaoRuxing 2009-6-30下午03:31:41
 * @version 1.0
 */
public class DatabaseType extends DataOperator{
	
	/** 数据库类型表表名 */
	private static final String tName="DB_TYPE";
	
	public DatabaseType() throws ErrorMessage {
		super(tName);
	}

	/**
	 * 实现类的交换器，根据交换代码执行相应的方法生成并执行SQL语句，返回执行结果
	 * @return 交换代码对应Sql的执行结果
	 * @throws ErrorMessage
	 */
	protected OperatorResult _exchange() throws ErrorMessage {
		ArrayList<String> sql=new ArrayList<String>();
		if(opId.equals(DatabaseTypeCode.Get_All_Db_Type)){
			this.getdDbType(sql);
			return this.doing(sql,Operator_Type.Select);
		}else if(opId.equals(DatabaseTypeCode.Get_Db_Type_By_Name)){
			this.getDbTypeByName(sql);
			return this.doing(sql,Operator_Type.Select);
		}else if(opId.equals(DatabaseTypeCode.Get_Db_Type_By_ID)){
			this.getDbTypeByID(sql);
			return this.doing(sql,Operator_Type.Select);
		}else{
			throw new ErrorMessage("Data Operator id not found:"+ opId+" !");
		}
	}
	
	/**
	 *  根据类型名称查询数据库类型
	 * @return  
	 * @throws ErrorMessage
	 */
	public void getDbTypeByName(ArrayList<String> sql) throws ErrorMessage {
		StringBuilder sp=new StringBuilder();
		sp.append(" select  db_type_id, db_type_name, db_type_desc  ");
		sp.append("   from  "+this.tableName+"  t       ");
		sp.append(" where db_type_name = '"+filter.getValue(MdStruc.DbTypeStruc.TYPE_NAME)+"'");
		sql.add(sp.toString());
	}
	
	/**
	 *  查询数据库类型
	 * @return  所有数据库类型
	 * @throws ErrorMessage
	 */
	public void getdDbType(ArrayList<String> sql) throws ErrorMessage {
		StringBuilder sp=new StringBuilder();
		sp.append(" select  db_type_id, db_type_name, db_type_desc  ");
		sp.append("   from  "+this.tableName+"  t       ");
		sql.add(sp.toString());
	}
	
	/**
	 *  根据类型ID称查询数据库类型
	 * @return  
	 * @throws ErrorMessage
	 */
	public void getDbTypeByID(ArrayList<String> sql) throws ErrorMessage {
		StringBuilder sp=new StringBuilder();
		sp.append(" select  db_type_id, db_type_name, db_type_desc  ");
		sp.append("   from  "+this.tableName+"  t       ");
		sp.append(" where db_type_id = '"+filter.getValue(MdStruc.DbTypeStruc.DATABASE_TYPE)+"'");
		sql.add(sp.toString());
	}
}
